package ren.kane.phoenix.util.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @author renrk(1275) on 2019-08-13 2:19 PM
 **/
public class StreamConverter {

    private static void testWrite() {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File("dir", "osw.txt")), "utf-8");
            outputStreamWriter.write("字节流转为字符流示例");
            outputStreamWriter.write("0123456789\n");
            outputStreamWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void testRead() {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File("dir", "osw.txt")), "utf-8");
            char c = (char) inputStreamReader.read();
            System.out.println("c=" + c);
            inputStreamReader.skip(6);
            char[] buf = new char[10];
            inputStreamReader.read(buf, 0, 10);
            System.out.println("buf=" + String.valueOf(buf));
            inputStreamReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        testWrite();
        testRead();
    }
}
